<?php
/**
 * Sync.php class file.
 * @author shengxia <523247758@qq.com>
 * @date 2021/03/05- 下午6:41
 */

namespace console\components\daemons\sync;


use common\exception\TaskException;
use console\components\helpers\ConsoleHelper;

class Task
{
    public $server;

    public $args = [];

    public $taskName;

    public function __construct(string $taskName,array $args = [])
    {
        $this->args = $args;

        $this->taskName = $taskName;

    }


    /**
     * 异步任务
     * @return mixed
     * @throws TaskException
     */
    public function run()
    {
        $classPath = "api\\task\\copy\\".$this->taskName;

        $this->className = $this->taskName;
        if(class_exists($classPath))
        {
            ConsoleHelper::runInfo('执行class:'.$classPath);
            return  (new $classPath($this->args))->run();
        }else{
            throw new TaskException('不存在任务【'.$this->taskName.'】，请完善好相应的异步任务脚本');
        }
    }
}